package com.yuan.mint.utils;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

public class BaiduLocUtils {
    private static LocationClient mLocationClient;
    private static BaiduLocUtils baiduLocUtils;

    private static String city;
    private static String locDetails;
    private static String locDescribe;
    //纬度
    public static double latitude;
    //经度
    public static double longitude;

    private static String cityInfo;

    public static BaiduLocUtils getInstance(Context context) {
        if (baiduLocUtils == null) {
            baiduLocUtils = new BaiduLocUtils(context);
        }
        return baiduLocUtils;
    }

    private BaiduLocUtils(Context context) {

        init(context);
        mLocationClient.start();
    }

    private void init(Context context) {
            mLocationClient = new LocationClient(context); //声明LocationClient类
            //设置定位条件
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true);        //是否打开GPS
            option.setIsNeedAddress(true);
            option.setCoorType("bd09ll");       //设置返回值的坐标类型。
            option.setPriority(LocationClientOption.NetWorkFirst);  //设置定位优先级
            option.setIsNeedLocationDescribe(true);
            mLocationClient.setLocOption(option);
    }

    public static void initListener(final LocCallBack callBack) {
        //注册监听函数
        mLocationClient.registerLocationListener(new BDLocationListener() {

            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                if (bdLocation == null) {
                    return;
                }
                latitude = bdLocation.getLatitude();
                longitude = bdLocation.getLongitude();
                locDescribe = bdLocation.getLocationDescribe();
                city = bdLocation.getCity();
                locDetails = bdLocation.getAddrStr();
                callBack.locCallBack(latitude, longitude, locDescribe, city, locDetails);
            }
        });
    }

    public interface LocCallBack {
        void locCallBack(double latitude, double longitude, String locDescribe, String city, String locDetails);
    }
}